package tech.abing.spider.task.pageprocessor.amac;

import java.sql.Timestamp;
import java.util.List;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import tech.abing.spider.component.PageProcessor;
import tech.abing.spider.component.PageScript;
import tech.abing.spider.model.Proccessable;
import tech.abing.spider.page.OkPage;
import tech.abing.spider.page.Page;
import tech.abing.spider.request.BasicRequest;
import tech.abing.spider.request.BinaryRequest;
import tech.abing.spider.request.StartContext;
import tech.abing.spider.task.constants.TaskConstants;
import tech.abing.spider.task.model.amac.AttachmentEntity;
import tech.abing.spider.task.model.amac.DisciplinaryActionEntity;
import tech.abing.spider.util.DateUtils;
import tech.abing.spider.util.StringUtils;
import tech.abing.spider.util.UuIdPublic;

/**
 * 解析处理纪律处分内容页面
 * @author Ivan qb.huang@wescxx.com   
 * @date 2016-11-3 上午09:44:07 
 * @version V2.0
 */
public class DisciplinaryContentProcessor implements PageProcessor {
	
	private static Logger logger = Logger.getLogger(DisciplinaryContentProcessor.class);
	
	private static int record = 0;

	@Override
	public PageScript getJavaScript() {
		return null;
	}

	@Override
	public Pattern getNormalContain() {
		return null;
	}

	@Override
	public void process(OkPage page, StartContext context, List<BasicRequest> queue,
			List<Proccessable> objectContainer) throws Exception {
		//System.out.println(page.getContent());
		
		record++;
		logger.info("正在解析第"+record+"条数据:");
		
		DisciplinaryActionEntity da = (DisciplinaryActionEntity)page.getRequest().getExtParams().get("da");
		
		Document doc = Jsoup.parse(page.getContent());
		
		da.setContent(doc.select("div.iRight").outerHtml());
		da.setAttachmentUrl(doc.select("a[filerela]").attr("href"));
		da.setId(UuIdPublic.timeStrongID(5));
		
		if(StringUtils.isNotEmpty(da.getAttachmentUrl())){
			AttachmentEntity attachment = new AttachmentEntity();
			String uuid = UuIdPublic.timeStrongID(5);
			attachment.setAttachType(TaskConstants.TASK_DISCIPLINARY_ACTION);
			attachment.setDownloadAddress(da.getAttachmentUrl());
			attachment.setFileName(uuid);
			attachment.setDisplayName(doc.select("a[filerela]").text());
			String savePath = "doc/"+DateUtils.getDate("yyyyMMdd")+"/"+attachment.getAttachType()+"/";
			attachment.setSavePath(savePath);
			attachment.setFileType("pdf");
			attachment.setCreateTime(new Timestamp(System.currentTimeMillis()));
			attachment.setId(uuid);
			StringUtils.removeSpace(attachment);
			objectContainer.add(attachment);
			
			BinaryRequest req = context.createBinaryRequest(da.getAttachmentUrl(), AttachmentProcessor.class);
			req.addAttribute("attachment", attachment);
			
			queue.add(req);
		}
		StringUtils.removeSpace(da);
		objectContainer.add(da);
	}

	@Override
	public void processErrorPage(Page page, StartContext context,
			List<Proccessable> objectContainer) throws Exception {
		// TODO Auto-generated method stub
		
	}


}
